<template>
  <div>
    <van-nav-bar
  title="黑马头疼-登录"
  fixed
  placeholder
  safe-area-inset-top
/>
<van-form @submit="onSubmit">
  <van-field
    v-model="form.mobile"
    name='mobile'
    label="手机号"
    placeholder="手机号"
    required
    :rules="[{ required: true, message: '请填写手机号',pattern: /^1[3-9]\d{9}$/}]"
  />
  <van-field
    v-model="form.code"
    name='code'
    type="password"
    label="验证码"
    placeholder="验证码"
    required
    :rules="[{ required: true, message: '请填写验证码',pattern: /^\d{6}$/}]"
  />
  <div style="margin: 16px;">
    <van-button
    :disabled ='isLogin'
    :loading='isLogin'
    loading-text="拼命加载中..."
    round
     block
     type="info"
     native-type="submit">登录</van-button>
  </div>
</van-form>
  </div>
</template>

<script>
import { loginAPI } from '@/api'
import { setToken } from '@/utils/token'
import { Notify } from 'vant'
export default {
  data () {
    return {
      form: {
        mobile: '13555555555',
        code: '246810'
      },
      isLogin: false
    }
  },
  methods: {
    async onSubmit (values) {
      this.lsLogin = true
      try {
        const res = await loginAPI({
          ...this.form
        })
        console.log(res)
        setToken(res.data.data.token)
        Notify({ type: 'success', message: '恭喜你!登录成功!' })
        this.isLogin = false
        this.$router.push({
          path: '/layout '
        })
      } catch (err) {
        console.log(err)
        Notify({ type: 'danger', message: '登录失败!' })
        this.isLogin = false
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
